import { http } from "@/utils/http";
import type {
  SysAdminCreateCommand,
  SysAdminRoleCreateCommand,
  SysAdminRoleSetStatusCommand,
  SysAdminRoleUpdateCommand,
  SysAdminUpdateCommand
} from "@/nswg-type/admin-module";

type Result = {
  success: boolean;
  data?: Array<any>;
};

type ResultTable = {
  success: boolean;
  data?: {
    /** 列表数据 */
    list: Array<any>;
    /** 总条目数 */
    total?: number;
    /** 每页显示条目个数 */
    pageSize?: number;
    /** 当前页数 */
    currentPage?: number;
  };
};

/** 获取系统管理-用户管理列表 */
// export const getUserList = (data?: object) => {
//   return http.request<ResultTable>("post", "/user", { data });
// };

/** 系统管理-用户管理-获取所有角色列表 */
// export const getAllRoleList = () => {
//   return http.request<Result>("get", "/list-all-role");
// };

/** 系统管理-用户管理-根据userId，获取对应角色id列表（userId：用户id） */
// export const getRoleIds = (data?: object) => {
//   return http.request<Result>("post", "/list-role-ids", { data });
// };

/** 获取系统管理-角色管理列表 */
// export const getRoleList = (data?: object) => {
//   return http.request<ResultTable>("post", "/role", { data });
// };

// /** 获取系统管理-菜单管理列表 */
// export const getMenuList = (data?: object) => {
//   return http
//     .request<Result>(
//       "get",
//       import.meta.env.VITE_API+ "/api/AdminModule/SysMenu/GetList",
//       { data }
//     )
//     .then(result => {
//       result.data = result.data.map(e => {
//         return { ...e, auths: e.permission, rank: e.sort };
//       });
//       return result;
//     });
// };

// export const addMenu = (data?: any) => {
//   data = { ...data, permission: data.auths, sort: data.rank };
//   return http.request<Result>(
//     "post",
//     import.meta.env.VITE_API+ "/api/AdminModule/SysMenu/Create",
//     { data }
//   );
// };

// export const editMenu = (data?: any) => {
//   data = { ...data, permission: data.auths, sort: data.rank };
//   return http.request<Result>(
//     "put",
//     import.meta.env.VITE_API+ "/api/AdminModule/SysMenu/Update",
//     { data }
//   );
// };

// export const delMenu = (data?: object) => {
//   return http.request<Result>(
//     "delete",
//     import.meta.env.VITE_API+ "/api/AdminModule/SysMenu/Delete",
//     { data }
//   );
// };

/** 获取系统管理-部门管理列表 */
export const getDeptList = (data?: object) => {
  return http.request<Result>("post", "/dept", { data });
};

/** 获取系统监控-在线用户列表 */
export const getOnlineLogsList = (data?: object) => {
  return http.request<ResultTable>("post", "/online-logs", { data });
};

/** 获取系统监控-登录日志列表 */
export const getLoginLogsList = (data?: object) => {
  return http.request<ResultTable>("post", "/login-logs", { data });
};

/** 获取系统监控-操作日志列表 */
export const getOperationLogsList = (data?: object) => {
  return http.request<ResultTable>("post", "/operation-logs", { data });
};

/** 获取系统监控-系统日志列表 */
export const getSystemLogsList = (data?: object) => {
  return http.request<ResultTable>("post", "/system-logs", { data });
};

/** 获取系统监控-系统日志-根据 id 查日志详情 */
export const getSystemLogsDetail = (data?: object) => {
  return http.request<Result>("post", "/system-logs-detail", { data });
};

/** 获取角色管理-权限-菜单权限 */
// export const getRoleMenu = (data?: object) => {
//   return http.request<Result>("post", "/role-menu", { data });
// };

// /** 获取角色管理-权限-菜单权限-根据角色 id 查对应菜单 */
// export const getRoleMenuIds = (data?: object) => {
//   return http.request<Result>("post", "/role-menu-ids", { data });
// };

//****************************** */
export const getMenuList = (data?: object) => {
  return http
    .request<Result>(
      "get",
      import.meta.env.VITE_API + "/api/AdminModule/SysMenu/GetList",
      { data }
    )
    .then(result => {
      console.log("getMenuList", result);
      result.data = result.data.map(e => {
        return { ...e, auths: e.permission, rank: e.sort };
      });
      return result;
    });
};

export const addMenu = (data?: any) => {
  data = { ...data, permission: data.auths, sort: data.rank };
  return http.request<Result>(
    "post",
    import.meta.env.VITE_API + "/api/AdminModule/SysMenu/Create",
    { data }
  );
};

export const editMenu = (data?: any) => {
  data = { ...data, permission: data.auths, sort: data.rank };
  return http.request<Result>(
    "put",
    import.meta.env.VITE_API + "/api/AdminModule/SysMenu/Update",
    { data }
  );
};

export const delMenu = (data?: object) => {
  return http.request<Result>(
    "delete",
    import.meta.env.VITE_API + "/api/AdminModule/SysMenu/Delete",
    { data }
  );
};

export const setMenuRole = (data?: any) => {
  console.log("setMenuRole", data);
  return http.request<Result>(
    "put",
    import.meta.env.VITE_API + "/api/AdminModule/SysAdminRole/SetMenu",
    { data }
  );
};

export const getRoleList = (params?: any) => {
  params = {
    PageIndex: params.PageIndex || 1,
    PageSize: params.PageSize || 10,
    Code: params.code || undefined,
    Name: params.name || undefined
  };
  return http.request<ResultTable>(
    "get",
    import.meta.env.VITE_API + "/api/AdminModule/SysAdminRole/GetPageList",
    { params }
  );
};

export const getAllRoleList = () => {
  return http.request<Result>(
    "get",
    import.meta.env.VITE_API + "/api/AdminModule/SysAdminRole/GetList"
  );
};

export const addRule = (data?: object) => {
  data = {
    ...data
  } as SysAdminRoleCreateCommand;
  return http.request<Result>(
    "post",
    import.meta.env.VITE_API + "/api/AdminModule/SysAdminRole/Create",
    { data }
  );
};

export const editRule = (data?: object) => {
  data = {
    ...data
  } as SysAdminRoleUpdateCommand;
  return http.request<Result>(
    "put",
    import.meta.env.VITE_API + "/api/AdminModule/SysAdminRole/Update",
    { data }
  );
};

export const delRule = (data?: object) => {
  return http.request<Result>(
    "delete",
    import.meta.env.VITE_API + "/api/AdminModule/SysAdminRole/Delete",
    { data }
  );
};

export const changeRuleStatus = (data?: object) => {
  data = {
    ...data
  } as SysAdminRoleSetStatusCommand;
  return http.request<Result>(
    "put",
    import.meta.env.VITE_API + "/api/AdminModule/SysAdminRole/SetStatus",
    { data }
  );
};

export const getRoleMenu = (params?: object) => {
  console.log(params);
  return http.request<Result>(
    "get",
    import.meta.env.VITE_API + "/api/AdminModule/SysMenu/GetList",
    {
      params
    }
  );
};

export const getRoleMenuIds = (params?: object) => {
  console.log(params);
  return http.request<Result>(
    "get",
    import.meta.env.VITE_API + "/api/AdminModule/SysAdminRole/GetMenuIds",
    {
      params
    }
  );
};

export const getUserList = (params?: any) => {
  params = {
    PageIndex: params.PageIndex || 1,
    PageSize: params.PageSize || 10,
    Code: params.code || undefined,
    Name: params.name || undefined
  };
  return http
    .request<ResultTable>(
      "get",
      import.meta.env.VITE_API + "/api/AdminModule/SysAdmin/GetPageList",
      { params }
    )
    .then(result => {
      result.data.list = result.data.list.map(e => {
        return {
          ...e,
          phone: e.phoneNumber,
          nickname: e.nickName,
          username: e.userName
        };
      });
      return result;
    });
};

export const addUser = (data?: any) => {
  data = {
    ...data,
    phoneNumber: data.phone,
    nickName: data.nickname,
    userName: data.username,
    higherDeptOptions: null
  } as SysAdminCreateCommand;
  return http.request<Result>(
    "post",
    import.meta.env.VITE_API + "/api/AdminModule/SysAdmin/Create",
    { data }
  );
};

export const editUser = (data?: any) => {
  data = {
    ...data,
    phoneNumber: data.phone,
    nickName: data.nickname,
    userName: data.username,
    higherDeptOptions: null
  } as SysAdminUpdateCommand;
  return http.request<Result>(
    "put",
    import.meta.env.VITE_API + "/api/AdminModule/SysAdmin/Update",
    { data }
  );
};

export const setUserRole = (data?: any) => {
  console.log("setUserRole", data);
  return http.request<Result>(
    "put",
    import.meta.env.VITE_API + "/api/AdminModule/SysAdmin/SetRole",
    { data }
  );
};

export const getUserRoleIds = (params?: object) => {
  return http.request<Result>(
    "get",
    import.meta.env.VITE_API + "/api/AdminModule/SysAdmin/GetUserRoleIds",
    { params }
  );
};

export const resetUserPassword = (data?: object) => {
  return http.request<Result>(
    "put",
    import.meta.env.VITE_API + "/api/AdminModule/SysAdmin/ResetPassword",
    { data }
  );
};
